Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup MKL direct factorizations #349

Merged
merged 5 commits into from
Aug 1, 2023
Merged

Setup MKL direct factorizations #349

merged 5 commits into from
Aug 1, 2023

Conversation

ChrisRackauckas
Copy link
Member

MWE:

using LinearSolve, MKL_jll
A = rand(4, 4); b = rand(4); u0 = zeros(4);
lp = LinearProblem(A, b);
truesol = solve(lp, LUFactorization())
mklsol = solve(lp, MKLLUFactorization())
@test truesol  mklsol

The segfault can be reproduced just with the triangular solver. MWE without LinearSolve:

using MKL_jll
using LinearAlgebra: BlasInt, LU
using LinearAlgebra.LAPACK: require_one_based_indexing, chkfinite, chkstride1, chkargsok
const usemkl = MKL_jll.is_available()

function getrf!(A::AbstractMatrix{<:Float64}; ipiv = similar(A, BlasInt, min(size(A,1),size(A,2))), info = Ref{BlasInt}(), check = false)
    require_one_based_indexing(A)
    check && chkfinite(A)
    chkstride1(A)
    m, n = size(A)
    lda  = max(1,stride(A, 2))
    ccall((:dgetrf_, MKL_jll.libmkl_rt), Cvoid,
            (Ref{BlasInt}, Ref{BlasInt}, Ptr{Float64},
            Ref{BlasInt}, Ptr{BlasInt}, Ptr{BlasInt}),
            m, n, A, lda, ipiv, info)
    chkargsok(info[])
    A, ipiv, info[] #Error code is stored in LU factorization type
end

A = rand(4,4); b = rand(4)
getrf!(A)
LU(getrf!(A)...) \ b

MWE:

```julia
using LinearSolve, MKL_jll
A = rand(4, 4); b = rand(4); u0 = zeros(4);
lp = LinearProblem(A, b);
truesol = solve(lp, LUFactorization())
mklsol = solve(lp, MKLLUFactorization())
@test truesol ≈ mklsol
```

The segfault can be reproduced just with the triangular solver. MWE without LinearSolve:

```julia
using MKL_jll
using LinearAlgebra: BlasInt, LU
using LinearAlgebra.LAPACK: require_one_based_indexing, chkfinite, chkstride1, chkargsok
const usemkl = MKL_jll.is_available()

function getrf!(A::AbstractMatrix{<:Float64}; ipiv = similar(A, BlasInt, min(size(A,1),size(A,2))), info = Ref{BlasInt}(), check = false)
    require_one_based_indexing(A)
    check && chkfinite(A)
    chkstride1(A)
    m, n = size(A)
    lda  = max(1,stride(A, 2))
    ccall((:dgetrf_, MKL_jll.libmkl_rt), Cvoid,
            (Ref{BlasInt}, Ref{BlasInt}, Ptr{Float64},
            Ref{BlasInt}, Ptr{BlasInt}, Ptr{BlasInt}),
            m, n, A, lda, ipiv, info)
    chkargsok(info[])
    A, ipiv, info[] #Error code is stored in LU factorization type
end

A = rand(4,4); b = rand(4)
getrf!(A)
LU(getrf!(A)...) \ b
```
Copy link

@ai-maintainer ai-maintainer bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-Maintainer Review for PR - WIP: Setup MKL direct factorizations

Title and Description 👍

The Title and Description are clear but could be more detailed
The title and description of the pull request do provide some explanation of the purpose of the changes. The title indicates that it is a work in progress (WIP) for setting up MKL direct factorizations. The description includes a minimal working example (MWE) that demonstrates the issue and provides code snippets to reproduce the segfault. However, the description could be more detailed and provide additional context about why these changes are necessary and what problem they aim to solve.

Scope of Changes 👍

The changes are narrowly focused
The changes in this pull request appear to be narrowly focused on setting up MKL direct factorizations. The author is not trying to resolve multiple issues simultaneously. The changes are specific to integrating MKL functionality into the LinearSolve package and addressing the segfault that occurs with the triangular solver.

Documentation 👍

All new functions, classes, or methods have docstrings
Yes, all newly added functions, classes, or methods have docstrings describing their behavior, arguments, and return values. There are no missing docstrings in this pull request.

Testing 👍

The author has provided a test case, but more details would be helpful
Yes, the description includes a description of how the author tested the changes. It provides a minimal working example (MWE) that demonstrates the issue and includes code snippets to reproduce the segfault. The author also includes a test assertion (`@test truesol ≈ mklsol`) to verify that the MKL-based solution is approximately equal to the true solution. However, it would be helpful if the author provided more details about the testing process, such as any additional test cases or scenarios that were considered.

Suggested Changes

  • Please provide more context in the PR description about why these changes are necessary and what problem they aim to solve.
  • Please provide more details about the testing process, such as any additional test cases or scenarios that were considered.

Reviewed with AI Maintainer

@ChrisRackauckas
Copy link
Member Author

@staticfloat or @chriselrod maybe you might have an idea of what I'm doing wrong here to cause a segfault?

@codecov
Copy link

codecov bot commented Jul 31, 2023

Codecov Report

Merging #349 (aaf64d3) into main (98a2292) will decrease coverage by 10.60%.
The diff coverage is 88.46%.

@@             Coverage Diff             @@
##             main     #349       +/-   ##
===========================================
- Coverage   75.29%   64.70%   -10.60%     
===========================================
  Files          17       18        +1     
  Lines        1267     1292       +25     
===========================================
- Hits          954      836      -118     
- Misses        313      456      +143     
Files Changed Coverage Δ
src/LinearSolve.jl 84.09% <50.00%> (-1.63%) ⬇️
ext/LinearSolveMKLExt.jl 91.30% <91.30%> (ø)
src/extension_algs.jl 12.50% <100.00%> (-57.07%) ⬇️

... and 4 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

ipiv = similar(A, BlasInt, min(size(A,1),size(A,2)))
end

ccall((:dgetrf_, MKL_jll.libmkl_rt), Cvoid,
Copy link
Contributor

@chriselrod chriselrod Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@staticfloat
Copy link
Contributor

staticfloat commented Jul 31, 2023

You've got MKL in 32-bit mode, but passing it Int64's, so the pivot indices that come back are gibberish, which causes the future LU factorization to have bogus indices, and the \ to segfault:

julia> A, ipiv, info = getrf!(A);

julia> ipiv
4-element Vector{Int64}:
  8589934595
 17179869187
           0
           0

There are a bunch of ways to fix this, but the best is to import @blasfunc (remember that guy we dropped when copy-pasting) from LinearAlgebra.BLAS and use that via @blasfunc(dgetrf_). That's good because you're already polymorphic by using similar(..., BlasInt, ...) when initializing ipiv, so this just makes it match up to the correct version by default.

@ChrisRackauckas ChrisRackauckas changed the title WIP: Setup MKL direct factorizations Setup MKL direct factorizations Aug 1, 2023
@ChrisRackauckas ChrisRackauckas merged commit 1ee467b into main Aug 1, 2023
14 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants